import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/login',
    component: _ => import('@/views/login/index.vue')
  },
  {
    path: '/home',
    component: _ => import('@/views/Home/Home.vue'),
    redirect: '/welcome',
    children: [
      {
        path: '/welcome',
        component: _ => import('@/views/welcome/Welcome.vue')
      },
      {
        path: '/users',
        component: _ => import('@/components/users')
      },
      {
        path: '/roles',
        component: _ => import('@/components/power/Roles.vue')
      },
      {
        path: '/rights',
        component: _ => import('@/components/power/Right.vue')
      },
      {
        path: '/goods',
        component: _ => import('@/components/goods/List.vue')
      },
      {
        path: '/params',
        component: _ => import('@/components/goods/Params.vue')
      },
      {
        path: '/categories',
        component: _ => import('@/components/goods/Cate.vue')
      },
      {
        path: '/orders',
        component: _ => import('@/components/order')
      }, {
        path: '/reports',
        component: _ => import('@/components/report')
      },
      {
        path: '/add',
        component: _ => import('@/components/goods/add.vue')
      }
    ]
  }
]

const router = new VueRouter({
  routes
})

// 路由守卫
router.beforeEach((to, from, next) => {
  // 如果是从login页面来的就放行
  if (to.path === '/login') return next()
  // 如果缓存中没有token就进行拦截返回到登录页面
  const tokenStr = window.sessionStorage.getItem('token')
  if (!tokenStr) return next('/login')
  next()
})
export default router
